package com.etrans.web.gps.business.report.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.bson.Document;
import org.springframework.stereotype.Service;

import com.etrans.web.gps.base.service.BaseService;
import com.etrans.web.gps.common.MGTemplete;
import com.etrans.web.gps.timer.LocalCache;
import com.mongodb.BasicDBObject;
import com.mongodb.client.FindIterable;

/**
 *报警综合分析Service
 * @author zhangw
 *
 */
@Service
public class PlatAlarmService extends BaseService{

	private final static String TABLE = "alarm_analyse_";
	
	/**
	 * 根据条件查询数据总数
	 * @param filter
	 * @param monthStr
	 * @return
	 */
	public int getTableDataCount(BasicDBObject query, String monthStr) {
		return MGTemplete.findCount(DBNAME, TABLE + monthStr, query).intValue();
	}

	/**
	 * 根据条件进行分页查询
	 * @param filter
	 * @param monthStr
	 * @param page
	 * @param pageSize
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public List<Document> getTableDataList(BasicDBObject query, String monStr, int page, int size) {
		BasicDBObject sort = new BasicDBObject("alarm_time",-1);
		FindIterable<Document> docs = MGTemplete.getCollection(DBNAME, TABLE+monStr).find(query).sort(sort).skip(page).limit(size);
		List<Document> list = new ArrayList<Document>();
		Map<Integer, String> entMap = LocalCache.entMap;
		Map<Integer, String> platMap = LocalCache.platMap;
		for (Document doc : docs) {
			doc.put("enterprise_name", entMap.get(doc.getInteger("enterprise_id")));
			doc.put("platform_name", platMap.get(doc.getInteger("platform_id")));
			doc.put("alarm_src", 3);
			doc.replace("alarm_type", doc.getString("alarm_name"));
			Document coordinates = (Document) doc.get("coordinates");
			List arr = (ArrayList) coordinates.get("coordinates");
			doc.put("lng", arr.get(0));
			doc.put("lat", arr.get(1));
            list.add(doc);
        }
		return list;
	}
	
}
